package pkg

import capi "github.com/hashicorp/consul/api"

type ConsulClient struct {
	client *capi.Client
}

func NewConsul(addr string) *ConsulClient {
	cfg := capi.DefaultConfig()
	cfg.Address = addr
	client, err := capi.NewClient(cfg)
	if err != nil {
		panic(err)
	}
	return &ConsulClient{client: client}
}

type Aservice struct {
	Name    string
	Address string
	Id      string
	Port    int
}

func (c *ConsulClient) Register(cfg Aservice) error {
	as := capi.AgentServiceRegistration{
		ID:      cfg.Id,
		Name:    cfg.Name,
		Port:    cfg.Port,
		Address: cfg.Address,
	}
	return c.client.Agent().ServiceRegister(&as)
}
